//
//  HomeViewController.swift
//  HR
//
//  Created by zhenyu on 17/6/1.
//  Copyright © 2017年 allblue. All rights reserved.
//

import UIKit

class HomeViewController: ViewController {
  
  weak var tableView: UITableView!
  var tableHeaderView = HomeHeader(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 132))
  
  // MARK: - LifeCycle
  override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.delegate = self
    setViews()
    // Do any additional setup after loading the view.
  }
  
  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }
  
  
  // MAKR: - SetViews
  func setViews() {
    
    let topView = HomeTopView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 84))
    view.addSubview(topView)
    
    tableView = {
      let t = UITableView(frame: CGRect(x: 0, y: 84, width: ScreenWidth, height: view.height - 84 - 48), style: .plain)
      view.addSubview(t)

      t.backgroundColor = Color.back
      t.rowHeight = UITableViewAutomaticDimension
      t.estimatedRowHeight = 100
      t.separatorColor = Color.line
      
      t.tableHeaderView = tableHeaderView
      t.showsVerticalScrollIndicator = false
      t.delegate = self
      t.dataSource = self
      t.separatorStyle = .none
      t.register(HomeCell.self, forCellReuseIdentifier: HomeCell.identifier())
      return t
    }()
  }
}


// MARK: - UITableViewDelegate, UITableViewDataSource
extension HomeViewController: UITableViewDelegate, UITableViewDataSource {
  func numberOfSections(in tableView: UITableView) -> Int {
    return 1
  }
  
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 2
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: HomeCell.identifier(), for: indexPath) as! HomeCell
    return cell
  }
  
  func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 0.1
  }
  
  func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 50
  }
  
  func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return HomeSectionView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 50))
  }
  
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    loginService.show()
  }
}


// MARK: - UINavigationControllerDelegate
extension HomeViewController: UINavigationControllerDelegate {
  // 隐藏导航栏
  func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
    let show = viewController.isKind(of: self.classForCoder)
    navigationController.setNavigationBarHidden(show, animated: show)
  }
}
